<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>生产领料编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
	<script src="${ctx}/js/print/LodopFuncs.js" type="text/javascript"></script>
<script type="text/javascript">
var basic_Url="${ctx}/logic/proget";
var grid;
$(function(){
$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
            
        //alert(jsonInfo); 
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
        
            getReturnMessage(url,"inputForm",W.searchAP);
        }catch(e){alert(e);}
    }
 }
);
if ('${viewflag}' == "add"){
	$("#tr1").hide();
	$("#save1").hide();
	$("#save2").hide();
	$("#save3").hide();
}else if ('${viewflag}' == "tijiao"){
	$("#save").hide();
	$("#save2").hide();
	$("#save3").hide();
      if($("#zd006Aegnt").val()!="${user.name}"){
		$("#save1").hide();
		$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
		$("select").attr("disabled",true);//禁用SELECT标签
		$("textarea").attr("disabled",true);//禁用TEXTAREA标签
      }  
}else if ('${viewflag}' == "shenpi"){
	$("#save").hide();
	$("#save1").hide();
	$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
	$("select").attr("disabled","disabled");//禁用SELECT标签
	$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
}else if ('${viewflag}' == "tongguo"){
	$("#save1").hide();
	$("#save2").hide();
	$("#save3").hide();
	$("#save").hide();
	$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
	$("select").attr("disabled","disabled");//禁用SELECT标签
	$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
}
grid = $("#dataGrid").ligerGrid({
    columns: [{ display: 'ID', name: 'id', width: 1 , hide:true},
              { display: '产品id', name: 'zd002MaterialsId', width: 1 , hide:true},
              { display: '<font color="gray">产品编码</font>', name: 'zd022ProCode',  width: 80},
			  { display: '<font color="gray">产品名称</font>', name: 'zd023ProName', width: 100 },
			  { display: '<font color="gray">产品数量</font>', name: 'zd018PlanQuantity', width: 80},
			  { display: '<font color="gray">材料编码</font>', name: 'zd002MaterialsCode', width: 100},
			  { display: '<font color="gray">材料名称</font>', name: 'zd002MaterialsName', width: 100},
			  { display: '<font color="gray">材料规格</font>', name: 'zd004MaterialsModel', width: 100},
			  { display: '<font color="gray">材料尺寸</font>', name: 'zd015MaterialsSize', width: 100},
			  { display: '<font color="gray">材料材质</font>', name: 'zd017MaterialsQuality', width: 100},
			  { display: '<font color="gray">材料描叙</font>', name: 'zd014MaterialsDescription',width: 1 , hide:true},
			  { display: '<font color="gray">材料单位</font>', name: 'zd006Unit', width: 60},
			  { display: '<font color="gray">BOM材料用量</font>', name: 'zd019BomQuantity', width: 80},
			  { display: '<font color="gray">库存数量</font>', name: 'zd024WhQuanlity', width: 80},
			  { display: '<font color="gray">已领料数量</font>', name: 'zd020WareGetQuantity', width: 80},
			  { display: '领料数量<font color="red">*</font>', name: 'zd007Quantity', width: 80,editor : {type :'float'}}
              ],
              enabledEdit: true,
              usePager: true,pageSize:1000,pageSizeOptions:[1000],
              parms:[{ name : 'id' , value : '${proget.id}'}],
              url : basic_Url+"/griditems.do",
              width: "100%", height:'90%', checkbox: true,
              toolbar: { items: [
                                 { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                 ]
                       }
 });
}); 
//生产计划单列表
function PlanWindow(){
	var zd004WhCode =$("#zd004WhCode").val();
	if(zd004WhCode==null||zd004WhCode==""){
		$.dialog.alert("请先选择要出库的仓库");
		return false;
	}
		getDG().dialog(
				{
					title : '选择生产计划单',
					width : 650,
					height : 350,
					content : 'url:' + sys_context
							+ '/public/windowalert/plan.do?type=2',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if(rows==null){
							$.dialog.alert("请选择数据!");
							return;
						}else{
							if(rows.length>1){
								$.dialog.alert("只能选择一行数据!");
								return;
							}else{
								$(rows).each(function() {
									$("#zd002OriCode").val(this.zd001FormCode);
									$("#zd003PiCode").val(this.zd003PiCode);
									var url=basic_Url+"/gridBomitems.do?id="+this.id+"&whcode="+zd004WhCode;
									multipleSearch("#dataGrid",true,"",url);
								});
							}
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
}
//删除行
function deleteRow()
{ 
	grid.deleteSelectedRow();
}

function getA(){
	$("#zd004WhName").val($("#zd004WhCode").find("option:selected").text());
	var manager = $("#dataGrid").ligerGetGridManager(); 
	manager.loadData(); 
}

function check(){
	$.ajax({
	      url: "${ctx}/logic/proget/updateState.do",
	      global: false,
	      type: "POST",
	      dataType: "json",
	      data: ({
	    	   rows:$("#id").val()
	      }),		     
	      success: function(msg){
	    	  if(msg.msg=="SUCCESS"){
	    		  $.dialog.alert("驳回成功",function(){close();});
	    	  }
	    	  if(msg.msg=="ERROR"){
	    		  $.dialog.alert("操作失败",function(){close();});
	    	  }
	   }
	});
}

function submitForm(param){
	$("#paramflag").val(param);
	var data = grid.getData();
	if (data.length == 0) {
		$.dialog.alert("您没有选择具体生产的产品，无法操作！");
		return false;
	}
	var ba = true;
	if(data !=null){ 
		for (var i = 0;i < data.length;i++){
			var row = data[i];
			if(undefined == row.zd007Quantity
					|| isNaN(row.zd007Quantity)
					|| row.zd007Quantity <= 0){
	           	 $.dialog.alert("您第"+(i+1)+"行的“领料数量”不合法");
	           	 ba=false;
	           	 return false;
	        }
			if(((row.zd018PlanQuantity*row.zd019BomQuantity)-row.zd007Quantity-row.zd020WareGetQuantity)<0){
				$.dialog.alert("您第"+(i+1)+"行的“领料数量”大于“订单数量”不合法");
	           	 ba=false;
	           	 return false;
			}
			if(row.zd007Quantity>row.zd024WhQuanlity){
				$.dialog.alert("您第"+(i+1)+"行的“领料数量”大于“库存数量”不合法");
	           	 ba=false;
	           	 return false;
			}
		}
	}
	jQuery.ajaxSettings.async = false;
	if (ba) {
		$("#itemdata").val(JSON.stringify(data));
		$("#submitFlag").val(param);
		$("#inputForm").submit();
	}
 }
 
function LodopPrint(data,type){
	$("#tb tr:not(:first)").remove(); 
	var t=0;
	for(var i=0;i<data.length;i++){
		var tr="<tr>";
		tr+='<td width="80px;" height="26px;" style="font-size:12px;" >'+$("#zd004WhName").val()+'</td>';
		tr+='<td width="80px;" height="26px;" style="font-size:12px;">'+data[i].zd002MaterialsCode+'</td>';
		tr+='<td width="80px;" height="26px;" style="font-size:12px;">'+data[i].zd002MaterialsName+'</td>';
		tr+='<td width="80px;" height="26px;" style="font-size:12px;">'+data[i].zd004MaterialsModel+'</td>';
		tr+='<td width="80px;" height="26px;" style="font-size:12px;">'+data[i].zd006Unit+'</td>';
		tr+='<td width="80px;" height="26px;" style="font-size:12px;">'+data[i].zd007Quantity+'</td>';
		tr+='<td width="80px;" height="26px;" style="font-size:12px;">'+data[i].zd014MaterialsDescription+'</td>';
		tr+="</tr>";
		$("#tb tr:eq("+i+")").after(tr);
		t++;
	}
	if(t<9){
		for(var i=t;i<9;i++){
			var tr="<tr>";
			tr+='<td width="80px;" height="26px;"></td>';
			tr+='<td width="80px;" height="26px;"></td>';
			tr+='<td width="80px;" height="26px;"></td>';
			tr+='<td width="80px;" height="26px;"></td>';
			tr+='<td width="80px;" height="26px;"></td>';
			tr+='<td width="80px;" height="26px;"></td>';
			tr+='<td width="80px;" height="26px;"></td>';
			tr+="</tr>";
			$("#tb tr:eq("+i+")").after(tr);
		}
	}
	$("#tb tr:eq(9)").after('<tr><td width="80px;" height="26px;"  colspan="7"></td></tr>');
	LODOP.PRINT_INIT("生产领料单");
	LODOP.ADD_PRINT_TEXT(68,200,352,30,"佛山市高尔顿家具有限公司");
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",19);
	LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
	LODOP.SET_PRINT_STYLEA(0,"Bold",1);
	LODOP.ADD_PRINT_TEXT(102,340,100,26,"领料单");
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",16);
	LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
	LODOP.ADD_PRINT_TABLE(170,19,680,295,document.getElementById("dv").innerHTML);
	LODOP.ADD_PRINT_TEXT(145,21,219,25,"PI编号："+$("#zd003PiCode").val());
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(145,284,162,25,"日期："+$("#zd010OtherDate").val());
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(145,500,200,25,"单据编号："+$("#zd001GetCode").val());
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(438,23,100,20,"仓库主管：");
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(438,206,100,20,"领料人："+$("#zd018GetUser").val());
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(436,384,100,20,"制单："+$("#zd006Aegnt").val());
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(484,38,224,20,"一.财务（白）");
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(484,287,233,20,"二.仓库（黄）");
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	LODOP.ADD_PRINT_TEXT(484,559,167,20,"三.车间（红）");
	LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
	LODOP.SET_PRINT_STYLEA(0,"FontSize",11);
	if(type==1){
		LODOP.PREVIEW();
	}else{
		LODOP.PRINT_SETUP();
	}
	
}

function PrintView(type){
	var data = grid.getData();//获取单据要打印物料
	if(data.length==0){
		$.dialog.alert("没有要打印的物料，请保存之后再打印");
		return false;
	}
	LODOP=getLodop();
	var rs=new Array();
	for(var i=1;i<=data.length;i++){
		rs.push(data[i-1]);
		if((i%9==0&&i!=1)||i==data.length){
			LodopPrint(rs,type);
			rs=new Array();
		}
	}
}
//出库仓库
function  SotrageinfoWindow() {
	getDG().dialog(
			{
				title : '选择仓库',
				width : 650,
				height : 350,
				content : 'url:' + sys_context + '/public/windowalert/sotrageinfo.do',    
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					if(rows==null){
						$.dialog.alert("请选择数据");
						return;
					}
					else{
						if(rows.length>1){
							$.dialog.alert('只能选择一行数据!');
							return;
						}else{
							$(rows).each(function() {
								$("#zd004Whid").val(this.id);
								$("#zd004WhCode").val(this.zd002Code);
								$("#zd004WhName").val(this.zd003Name);
						  });
						}
					}
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${proget.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${proget.bizVersion}"/>
<input type="hidden" id="itemdata" name="itemdata"/>
<input type="hidden" id="paramflag" name="paramflag"/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         <table class="TableBlock" align="center">
         			<c:if test="${not empty proget.zd001GetCode}">
         			<tr>
                    <td>生产领料单号:</td>
   		                 <td colspan="3">
   		                <input type="text" id="zd001GetCode" name="zd001GetCode" readonly="readonly" style="width: 40%;"  value="${proget.zd001GetCode}" />
   		                 </td>
   		           </tr>
   		           </c:if>
   		           <tr>
						<td>出库仓库:</td>
						<td>
   		             		 <input type="hidden" id="zd004WhCode" name="zd004WhCode" value="${proget.zd004WhCode}"/>
   		                     <input type="text" id="zd004WhName" name="zd004WhName" value="${proget.zd004WhName}"/>
	   		            	 <input type="hidden" style="width:200px;" id="zd004Whid" name="zd004Whid" value="${proget.zd004Whid}" />
   		        			<input type="button" value="..." onclick="SotrageinfoWindow();" />
						</td>
					 	<td>出库时间:</td>
						<td><input id="zd010OtherDate" name="zd010OtherDate" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${proget.zd010OtherDate}" pattern="yyyy-MM-dd" type="both" />" />
							<c:if test="${proget.zd010OtherDate == null}">
									<script>
										var date = new Date();
										$("#zd010OtherDate").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
									</script>
							 </c:if>
						</td>
					</tr>
   		           <tr>
   		                  <td >生产计划单号:</td>
   		                 <td>
   		                <input type="text" id="zd002OriCode" name="zd002OriCode" class="required" readonly="readonly" style="width: 40%;" value="${proget.zd002OriCode}" />
   		                 <input type="button" value="..." onclick="PlanWindow();"/>
   		                 </td>
   		                  <td >PI编号:</td>
   		                 <td>
   		                <input type="text" id="zd003PiCode" name="zd003PiCode" class="required" readonly="readonly" style="width: 40%;" value="${proget.zd003PiCode}" />
   		                </td>
   		           </tr>
   		           <tr>
   		                  <td >领料人:</td>
   		                 <td>
   		                <input type="text" id="zd018GetUser" name="zd018GetUser" class="required" style="width: 40%;" value="${proget.zd018GetUser}" />
   		                 </td>
   		                  <td >经办人:</td>
   		                 <td>
   		                <input type="text" id="zd006Aegnt" name="zd006Aegnt" class="required" style="width: 40%;" value="${proget.zd006Aegnt}" />
   		                <c:if test="${not empty proget.zd006Aegnt}">
   		                <script type="text/javascript">
   		             		$("#zd006Aegnt").val('${user.name}');
   		                </script>
   		                </c:if>
   		                </td>
   		           </tr>
   		           <tr>
   		           	<td >备注:</td>
   		           	<td colspan="3"><input id="zd007Remark" name="zd007Remark" style="width: 40%;" value="${proget.zd007Remark}" /></td>
   		           </tr>
   		           <tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid"></div></td>
					</tr>
                </table>               
         </div>
         <div class="formBar">
	        <ul>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('PROGET_SAVE')">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm('PROGET_AUDIT')">提交</button></div></div></li>
		        <webres:displaycontroller elementId="LOGIC_PROGET_SC_SP" description="生产领料单审批">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitForm('PROGET_END')">审核通过</button></div></div></li>
			    </webres:displaycontroller>
			    <webres:displaycontroller elementId="LOGIC_PROGET_SC_PH" description="生产领料单驳回">   
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="check()">驳回</button></div></div></li>
		       </webres:displaycontroller>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(1);">打印预览</button></div></div></li>
		      	<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(2);">打印</button></div></div></li>
				</ul>
        </div>    
    </div>
    
    <div id="dv" style="display:none">
<table id="tb" border=1 width="100%" cellspacing="0" cellpadding="0" style="border-collapse:collapse" bordercolor="#000000">
<tr>
<td width="80px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;库别</td>
<td width="80px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编号</td>
<td width="140px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;物料名称</td>
<td width="140px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;规格型号</td>
<td width="80px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;单位</td>
<td width="80px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数量</td>
<td width="80px;" height="26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;备注</td>
</tr>

</table>
</div>
</form>
</body>
</html>
